<?php

namespace App\Jobs;

use App\Http\Service\CountryService;
use App\Http\Service\HouseService;
use App\Model\Business;
use App\Model\ContractTenant;
use App\Model\House;
use App\Model\TjCountry;
use App\Models\Contract;
use App\Models\MemberCoupon;
use App\Models\Order;
use App\Models\Paper;
use App\services\ImmService;
use App\User;
use App\Utils\Result;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Storage;

class CheckContract implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    private $cId;
    public $tries = 3;
    public $timeout = 120;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($cId)
    {
        $this->cId = $cId;
        //因为要导入大文件 需要很久时间
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        try{
            Log::info('queue-contract:'.$this->cId);
            $contract = \App\Model\Contract::query()->where('id',$this->cId)->first();
            $business = Business::query()->where('id',$contract->business_id)->first();
            $user = User::query()->where('id',$business->user_id)->first();
            $h= House::query()->where('id',$contract->house_id)->first();
            if($h->country_code){
                $service = new CountryService();
               $tenants = ContractTenant::query()
                   ->where('contract_id',$contract->id)
                   ->where('is_push',0)->get();
               foreach($tenants as &$v){
                    $data['regionCode'] = $h->country_code;
                    $data['name'] = $v['name'];
                    $data['phone'] = $v['mobile'];
                    $data['address'] = $h->province.$h->city.$h->district.$h->estate_name.$h->name;
                    $data['idcard'] = $v['id_no'];
                    $data['idcardFront'] = $v['id_card_image1'];
                    $data['idcardBack'] = $v['id_card_image2'];
                    $data['renttimeStr'] = date('Y-m-d',strtotime($v->created_at));
                    $data['landlordName'] = $business->name?$business->name:'未实名';
                    $data['landlordPhone'] = $user->mobile;
                    $service->pushTenant($data);
                    $v->is_push = 1;
                    $v->save();
               }
            }
        }catch (\Exception $e){
            Log::info($e->getMessage());
        }

    }
}

